import numpy as np
import csv
import matplotlib.pyplot as plt

# BasePath = "D:/VR_project/LiveDeep_All/LiveDeep/normal/"
BasePath = "D:/VR_project/LiveDeep_All/LiveDeep/normal/demo/"

# FileList = ["1-1-Conan Gore Fly", "1-2-Front", "1-3-360 Google Spotlight Stories_ HELP", "1-4-Conan Weird Al",
#             "1-5-TahitiSurf", "1-6-Falluja", "1-7-Cooking Battle", "1-8-Football", "1-9-Rhinos",
#             "2-1-Korean", "2-2-VoiceToy", "2-3-RioVR", "2-4-FemaleBasketball", "2-5-Fighting", "2-6-Anitta",
#             "2-7-TFBoy", "2-8-reloaded"]
# nameList = ['Conan1', 'Skiing', 'Alien', 'Conan2', 'Surfing', 'War', 'Cooking', 'Rhinos']
# for idx, item in enumerate(FileList):
#     if idx == 7 or idx >= 9:
#         continue

nameList = ['Alien', 'Conan1', 'Conan2', 'Cooking', 'Rhinos', 'Skiing', 'Surfing', 'War']

frameAccList = []
tileAccList = []
recallList = []
precisionList = []
lowBandList = []
highBandList = []

for item in nameList:
    # nameList.append(item.split('-')[-1])
    # metrics = np.loadtxt(BasePath + item + "/metrics.csv", delimiter=',', dtype=str)

    # todo: 注意 metrics 还是 metrics2
    with open(BasePath + item + "/metrics2.csv", 'r') as csvfile:
        reader = csv.reader(csvfile)
        rows = [row for row in reader]

        frameAccList.append(round(float(rows[-6][1]), 4))
        tileAccList.append(round(float(rows[-5][1]), 4))
        recallList.append(round(float(rows[-4][1]), 4))
        precisionList.append(round(float(rows[-3][1]), 4))

        lowBandList.append(round(float(rows[-2][1]), 4))
        highBandList.append(round(float(rows[-1][1]), 4))

# TileMetrics
x = np.arange(len(nameList))
width = 0.2
plt.rcParams['font.size'] = 20
fig, ax = plt.subplots(figsize=(20, 8))
n = 0

plt.bar(x + width * n, tileAccList, width=width, label='AvgTileAccuracy')
plt.bar(x + width * (n+1), recallList, width=width, label='AvgRecall')
plt.bar(x + width * (n+2), precisionList, width=width, label='AvgPrecision')

plt.grid(linestyle='--')
plt.xticks(x + width, nameList, fontsize=20)
plt.ylim(0, 1)
# plt.yticks(np.arange(0, 1.2, 0.1), fontsize=20)
ax.set_ylabel('AverageMetrics')
ax.yaxis.set_major_locator(plt.MultipleLocator(0.1))

plt.legend(loc='center', ncol=4, bbox_to_anchor=[0.5, 0.95])
# plt.tight_layout()
plt.show()


# FrameAccuracy 柱状图
"""x = np.arange(len(nameList))
width = 0.5
fig, ax = plt.subplots()

plt.bar(x, frameAccList, width=width, label='FrameAccuracy')
plt.grid(linestyle='--')
plt.xticks(x, nameList)
plt.ylim(0, 1)
ax.set_ylabel('PredictionAccurancy')
ax.yaxis.set_major_locator(plt.MultipleLocator(0.1))
plt.legend()
# plt.tight_layout()
plt.show()
"""

# 带宽柱形图
"""x = np.arange(len(nameList))
width = 0.3
plt.rcParams['font.size'] = 20
fig, ax = plt.subplots(figsize=(20, 8))

plt.bar(x, lowBandList, width=width, label='Bandwidth with FOV')
plt.bar(x + width, highBandList, width=width, label='Bandwidth without FOV')

plt.grid(linestyle='--')
plt.xticks(x+ width/2, nameList, fontsize=20)
ax.set_ylabel('Average Bandwidth / Frame (bps)')
plt.legend(loc='center', ncol=2, bbox_to_anchor=[0.5, 0.95])
# plt.tight_layout()
plt.show()"""

print('matplotlib plot finish!')